<template>
  <div>
    <el-card id="table-block">
      <el-form :model="ruleForm"
               :rules="rules"
               ref="ruleForm"
               label-width="100px">
        <el-form-item label="姓名"
                      prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="区域"
                      prop="area">
          <el-input v-model="ruleForm.area"></el-input>
        </el-form-item>
        <el-form-item label="globalId"
                      prop="globalId">
          <el-input v-model="ruleForm.globalId"></el-input>
        </el-form-item>
        <el-button type="text"
                   @click="generatePassword">根据globalId自动生成同名密码</el-button>
        <el-form-item label="密码"
                      prop="password">
          <el-input v-model="ruleForm.password"
                    type="password"
                    show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码"
                      prop="confirmPassword">
          <el-input v-model="ruleForm.confirmPassword"
                    type="password"
                    show-password></el-input>
        </el-form-item>
        <el-form-item label="部门"
                      prop="department">
          <el-select v-model="ruleForm.department"
                     filterable
                     placeholder="请选择部门">
            <el-option v-for="(item,index) in departmentList"
                       :key="index"
                       :label="item.label"
                       :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-alert title="！！！打开权限后该用户将有权限新增用户（建议权限只赋予项目部门）"
                  type="warning"
                  :closable="false">
        </el-alert>
        <el-form-item label="权限"
                      prop="rights">
          <el-switch v-model="ruleForm.rights"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="submitForm('ruleForm')">创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        globalId: "",
        area: "",
        password: "",
        confirmPassword: "",
        department: "",
        rights: false
      },
      rules: {
        name: [{ required: true, message: "请输入用户姓名", trigger: "blur" }],
        globalId: [
          { required: true, message: "请输入globalId", trigger: "blur" }
          // { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        area: [{ required: true, message: "请输入区域", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        confirmPassword: [
          { required: true, message: "请确认密码", trigger: "blur" }
        ],
        department: [
          { required: true, message: "请选择部门", trigger: "change" }
        ]
      },
      departmentList: []
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.ruleForm.password != this.ruleForm.confirmPassword) {
            this.$message.error("两次密码不一样");
          } else {
            this.$api.userInfo
              .insertUser({
                userId: this.ruleForm.globalId,
                password: this.ruleForm.password,
                department: this.ruleForm.department,
                rights: this.ruleForm.rights,
                userName: this.ruleForm.name,
                area: this.ruleForm.area
              })
              .then(res => {
                if (res.data) {
                  this.$message.success("新增成功");
                  this.$refs[formName].resetFields();
                } else {
                  this.$message.error("新增失败，请重试");
                }
              });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getDepartment() {
      this.$api.userInfo
        .requestDepartments()
        .then(response => {
          this.departmentList = [];
          for (var i = 0; i < response.data.departments.length; i++) {
            this.departmentList.push({
              label: response.data.departments[i]
            });
          }
        })
        .catch(error => {
          console.log("error: ", error);
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    generatePassword() {
      this.ruleForm.password = this.ruleForm.globalId;
      this.ruleForm.confirmPassword = this.ruleForm.globalId;
    }
  },
  mounted() {
    this.getDepartment();
  }
};
</script>

<style lang="less" scope>
#table-block {
  width: 80%;
  margin: 50px auto;
  border-radius: 25px;
  padding: 50px 20px;
}
</style>
